/**
 * jQuery对象扩展
 */
(function($) {
    "use strict";

    //- 禁止右键点击
    $(document).bind("contextmenu", (e) => false);

    /**
     * animate.css 动画执行完毕事件
     * @param  {Function}    回调事件
     * @return {Object}      执行结果
     */
    $.fn.animateEnd = function(callback) {
        $(this).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
            if (callback) callback.call(this);
        });
    };

    $.fn.transitionEnd = function(callback) {
        var events = ['webkitTransitionEnd', 'transitionend', 'oTransitionEnd', 'MSTransitionEnd', 'msTransitionEnd'],
            i, len, dom = this;

        function fireCallBack(e) {
            if (e.target !== this) return;
            callback.call(this, e);
            for (i = 0, len = events.length; i < len; i++) {
                dom.off(events[i], fireCallBack);
            }
        }

        if (callback) {
            for (i = 0, len = events.length; i < len; i++) {
                dom.on(events[i], fireCallBack);
            }
        }
        return this;
    };

    /**
     * 复制到剪贴板
     */
    $.fn.copyClipboard = function() {
        var $this = $(this);
        if ($this.is('input') || $this.is('textarea')) {
            if ($this.val()) {
                //- 选中内容
                $this.select();
                //- 执行浏览器复制
                document.execCommand("Copy");
            }
        }
    };

    //- 剔除元素中的HTML
    //- $('p').stripHtml();
    $.fn.stripHtml = function() {
        var regexp = /&lt;("[^"]*"|'[^']*'|[^'"&gt;])*&gt;/gi;
        this.each(function() {
            $(this).html($(this).html().replace(regexp, ""));
        });
        return $(this);
    }

    //- 使元素居屏幕中间位置
    //- $("#id").center();
    $.fn.center = function() {
        return $window = $(window), this.css('position', 'absolute')
            .css('top', ($window.height() - this.height()) / 2 + $window.scrollTop() + 'px')
            .css('left', ($window.width() - this.width()) / 2 + $window.scrollLeft() + 'px');
    };

    //- 自动滚动到页面特定区域
    //- $('.area_name').autoscroll();
    $.fn.autoscroll = function(selector) {
        $('html,body').animate({ scrollTop: $(selector).offset().top }, 500);
    }

    //- 预加载图片
    //- $.preloadImages('img/hover1.png','img/hover2.png');
    $.preloadImages = function() {
        for (var i = 0, len = arguments.length; i < len; i++) {
            $('<img>').attr('src', arguments[i]);
        }
    };

})($);